cd "~/Dropbox/Shared Folder - J. Ladd & M (1). Meredith/Replication Files/"
cap log close
log using "LogFiles/FigureA2.log", replace

clear all
set more off

use "RawData/GallupDataBJPS.dta", clear

// Drops Unrepresentative Series
drop if drops == "All" | regexm(drops, "Party")
// Drops Obs. w/o Gender
drop if female < 0
// Drops Obs. w/o Weights
drop if final_weight < 0
// Drops Obs. w/o Dem., Ind., or Rep.
keep if party == -1 | party == 0 | party == 1
// Drops Phone Surveys
keep if survey == 1

// Sets the Survey Date as the Mid-Point in Field
gen middle_date = .
replace middle_date = floor((start_date + end_date) / 2)
sort middle_date
format middle_date %td

// Codes Dem. as 0, Ind. as 1/2, and Rep. as 1
gen partisan = 0 if party == -1
replace partisan = 1 if party == 1
replace partisan = .5 if party == 0

// Aggregates Weighted Partisanship for Females
egen total_partisan_female = sum(partisan * (female == 1) * final_weight), by(series)
// Aggregates Weighted Partisanship for Males
egen total_partisan_male = sum(partisan * (female == 0) * final_weight), by(series)
// Weighted Female Count
egen total_obs_female = sum((female == 1) * (final_weight)), by (series)
// Weighted Male Count
egen total_obs_male = sum((female == 0) * (final_weight)), by (series)
// Avg. Female Partisanship
gen partisan_female = total_partisan_female / total_obs_female
// Avg. Male Partisanship
gen partisan_male = total_partisan_male / total_obs_male 
// Difference in Female and Male Avg. Partisanship
gen partisan_difference = partisan_female - partisan_male

// Only Keeps One Obs. Per Survey
keep if obs_num == 1 

// Creates Fake Variable to Aid Plot
gen temp = 0

local lowerbound = mdy(1, 1, 1960)
local middlebound1 = mdy(1, 1, 1962)
local middlebound2 = mdy(1, 1, 1964)
local middlebound3 = mdy(1, 1, 1966)
local upperbound = mdy(1, 1, 1968)
local upperbound2 = `upperbound' + 90 

// Plots Evolution of Partisan Gender Gap in In-Person Surveys from 1960-1967
twoway (scatter partisan_difference middle_date if middle_date >= `lowerbound' /*
*/ & middle_date <= `middlebound2', mcolor(black) mfcolor(white) msize(small)) /*
*/ (scatter partisan_difference middle_date if middle_date > `middlebound2' /*
*/ & middle_date <= `upperbound', mcolor(gray) mfcolor(gray) msize(small)) /*
*/ (lfit partisan_difference middle_date if middle_date >= `lowerbound' /*
*/ & middle_date <= `middlebound2', lcolor(black) lwidth(thick)) /*
*/ (lfit partisan_difference middle_date if middle_date > `middlebound2' /*
*/ & middle_date <= `upperbound', lpattern(dash) lcolor(black) lwidth(thick) color(black)) /* 
*/ (scatter temp middle_date if middle_date >= `upperbound' /*
*/ & middle_date <= `upperbound2', mcolor(white) mfcolor(white) msize(small)), /*
*/ legend(rows(2) order(1 "Partisan Gender Gap in Survey (1960-1963)" /*
*/ 2 "Partisan Gender Gap in Survey (1964-1967)" 3 "Linear Regression (1960-1963)" /*
*/ 4 "Linear Regression (1964-1967)")) xsize(14) ysize(8) scale(.7) /*
*/ xlabel(`lowerbound' `middlebound1' `middlebound2' `middlebound3' `upperbound') /*
*/ xtitle({stSerif: Survey Date}) ytitle({stSerif: Difference in Females' and Males' Partisanship Level}) /*
*/ graphregion(fcolor(white)) ylabel(,nogrid)
graph export "TablesFigures/FigureA2a.eps", as(eps) replace

local lowerbound = mdy(1, 1, 1976)
local middlebound1 = mdy(1, 1, 1978)
local middlebound2 = mdy(1, 1, 1980)
local middlebound3 = mdy(1, 1, 1982)
local upperbound = mdy(1, 1, 1984)
local upperbound2 = `upperbound' + 90 

// Plots Evolution of Partisan Gender Gap in In-Person Surveys from 1976-1983
twoway (scatter partisan_difference middle_date if middle_date >= `lowerbound' /*
*/ & middle_date <= `middlebound2', mcolor(black) mfcolor(white) msize(small)) /*
*/ (scatter partisan_difference middle_date if middle_date > `middlebound2' /*
*/ & middle_date <= `upperbound', mcolor(gray) mfcolor(gray) msize(small)) /*
*/ (lfit partisan_difference middle_date if middle_date >= `lowerbound' /*
*/ & middle_date <= `middlebound2', lcolor(black) lwidth(thick)) /*
*/ (lfit partisan_difference middle_date if middle_date > `middlebound2' /*
*/ & middle_date <= `upperbound', lpattern(dash) lcolor(black) lwidth(thick) color(black)) /* 
*/ (scatter temp middle_date if middle_date >= `upperbound' /*
*/ & middle_date <= `upperbound2', mcolor(white) mfcolor(white) msize(small)), /*
*/ legend(rows(2) order(1 "Partisan Gender Gap in Survey (1976-1979)" /*
*/ 2 "Partisan Gender Gap in Survey (1980-1983)" 3 "Linear Regression (1976-1979)" /*
*/ 4 "Linear Regression (1980-1983)")) xsize(14) ysize(8) scale(.7) /*
*/ xlabel(`lowerbound' `middlebound1' `middlebound2' `middlebound3' `upperbound') /*
*/ xtitle({stSerif: Survey Date}) ytitle({stSerif: Difference in Females' and Males' Partisanship Level}) /*
*/ graphregion(fcolor(white)) ylabel(,nogrid)
graph export "TablesFigures/FigureA2b.eps", as(eps) replace

log close
